<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使用defineProperty</title>
</head>
<body>
  <script>
    const obj = {
      name: 'Alan',
      age: 32
    }
    // writable和get/set不能共存
    // 响应式原理,解释:https://www.jianshu.com/p/8fe1382ba135
    Object.keys(obj).forEach(key => {
      let value = obj[key]
      Object.defineProperty(obj, key, {
        enumerable: true, // 允许枚举
        configurable: true, // 允许修改
        get () {
          return value
        },
        set (val) {
          value = val
        }
      })
    })
    const defind = {}
    Object.defineProperty(defind, 'name', {
        enumerable: true, // 允许枚举
        configurable: true, // 允许修改
        writable: true,
        value: 'Alan'
      })
  </script>
</body>
</html>